//
//  WCPickerView.m
//  WuCha
//
//  Created by michael shen on 16/2/24.
//  Copyright © 2016年 youzi. All rights reserved.
//

#import "WCPickerView.h"

@interface WCPickerView()<UIPickerViewDelegate,UIPickerViewDataSource>{
    WCPickerViewBlock wcBlock;
    WCDatePickerBlock wcDateBlock;
    NSInteger chooseInteger;
}
@property (nonatomic,strong) UIPickerView *pickerView;
@property (nonatomic,strong) NSArray *firstArray;
//@property (nonatomic,strong) UIBarButtonItem *centerItem;
@property (nonatomic,strong) UILabel *centerLabel;
@property (nonatomic,strong) NSString *dateString;
@end

@implementation WCPickerView

-(id)initViewWithArray:(NSArray*)array{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        self.firstArray = array;
        [self ShowpickerViewWithSelectedIndex:0];
    }
    
    return self;
}

-(id)initWithDataPick{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        [self showDataPicker];
    }
    return self;
}


-(void)setWcDataPickSuccessWithBlock:(WCDatePickerBlock)block{
    wcDateBlock = block;
}

-(void)setWcPickSuccessWithBlock:(WCPickerViewBlock)block{
    wcBlock = block;
}

-(void)showDataPicker{
    if(_confirmView!=nil) {
        [_confirmView removeFromSuperview];
    }
    _confirmView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 324+64, SCREEN_HEIGHT, 260)];
    _confirmView.backgroundColor=DefaultOpenColor;
    
    [self addSubview:_confirmView];
   
    UIView *toolbarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.titleLabel.font = [UIFont systemFontOfSize:14];
    leftButton.frame = CGRectMake(10, 0, 50, 44);
    [leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [leftButton setTitle:@"取消" forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
    [toolbarView addSubview:leftButton];
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:14];
    rightButton.frame = CGRectMake(SCREEN_WIDTH-60, 0, 50, 44);
    [rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightButton setTitle:@"完成" forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(confirm:) forControlEvents:UIControlEventTouchUpInside];
    [toolbarView addSubview:rightButton];
    
//    self.centerItem = fitItem;
    
    self.centerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-110, 24)];//pt-140,en-180
    _centerLabel.backgroundColor = [UIColor clearColor];//brownColor
    _centerLabel.textColor = [UIColor whiteColor];
    _centerLabel.font = [UIFont systemFontOfSize:16];
    _centerLabel.textAlignment = NSTextAlignmentCenter;
    _centerLabel.hidden = YES;
    
    [_confirmView addSubview:toolbarView];
    
    UIDatePicker *datePicker= [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44,SCREEN_WIDTH, 216.0)];
    datePicker.backgroundColor = [UIColor whiteColor];
    //    if (self.chooseType == StartType) {
    //        [pickerView selectRow:startInteger?startInteger:0 inComponent:0 animated:YES];
    //    }else{
    //        [pickerView selectRow:endInteger?endInteger:0 inComponent:0 animated:YES];
    //    }
    datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    datePicker.datePickerMode = UIDatePickerModeDate;
    [datePicker addTarget:self action:@selector(dateChange:)forControlEvents:UIControlEventValueChanged];
    
    [_confirmView addSubview:datePicker];
}

-(void)dateChange:(UIDatePicker*)dataPicker{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    self.dateString = [dateFormatter stringFromDate:dataPicker.date];
}

-(void)ShowpickerViewWithSelectedIndex:(int)index
{
    if(_confirmView!=nil) {
        [_confirmView removeFromSuperview];
    }
    
    _confirmView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 324+64, SCREEN_HEIGHT, 260)];
    _confirmView.backgroundColor=DefaultOpenColor;
    
    [self addSubview:_confirmView];
    
    UIView *toolbarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.titleLabel.font = [UIFont systemFontOfSize:14];
    leftButton.frame = CGRectMake(10, 0, 50, 44);
    [leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [leftButton setTitle:@"取消" forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
    [toolbarView addSubview:leftButton];
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:14];
    rightButton.frame = CGRectMake(SCREEN_WIDTH-60, 0, 50, 44);
    [rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightButton setTitle:@"完成" forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(confirm:) forControlEvents:UIControlEventTouchUpInside];
    [toolbarView addSubview:rightButton];
//    self.centerItem = fitItem;
    
    self.centerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-110, 24)];//pt-140,en-180
    _centerLabel.backgroundColor = [UIColor clearColor];//brownColor
    _centerLabel.textColor = [UIColor orangeColor];
    _centerLabel.font = [UIFont systemFontOfSize:16];
    _centerLabel.textAlignment = NSTextAlignmentCenter;
    _centerLabel.hidden = YES;
    
    
    [_confirmView addSubview:toolbarView];
    
    self.pickerView= [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44,SCREEN_WIDTH, 216.0)];
    _pickerView.delegate = self;
    _pickerView.dataSource=self;
    _pickerView.backgroundColor = [UIColor whiteColor];
    _pickerView.showsSelectionIndicator = YES;
    //    if (self.chooseType == StartType) {
    //        [pickerView selectRow:startInteger?startInteger:0 inComponent:0 animated:YES];
    //    }else{
    //        [pickerView selectRow:endInteger?endInteger:0 inComponent:0 animated:YES];
    //    }
    [_confirmView addSubview:_pickerView];
}

#pragma mark- UIPickViewDelegate
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{

    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

    return self.firstArray.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    
    return self.firstArray[row];
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    chooseInteger = row;
    self.centerLabel.text = self.firstArray[row];
}

-(void)cancel:(id)sender{
    [self removeFromSuperview];
}

-(void)confirm:(id)sender{
    if (wcBlock) {
        wcBlock(chooseInteger);
    }
    if (wcDateBlock) {
        if (self.dateString) {
            wcDateBlock(self.dateString);
        }else{
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"yyyy-MM-dd"];
            self.dateString = [dateFormatter stringFromDate:[NSDate date]];
            wcDateBlock(self.dateString);

        }
    }
    [self removeFromSuperview];

}

@end
